<?php

namespace app\admin\controller\wechat;

use app\common\controller\Backend;

/**
 * 药品管理
 *
 * @icon fa fa-circle-o
 */
class Drafts extends Backend
{

    /**
     * Keyword模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wechat\Drafts;
        $this->modelAccount = new \app\admin\model\wechat\Accounts;
        $this->modelArticle = new \app\admin\model\wechat\Articles();
        $this->access_token = $this->modelAccount->getAccessToken();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $lists = $list->items();
            foreach ($lists as $k => $v) {
                $articles_ids = explode(',', $v['articles_ids']);
                $article_first_id = $articles_ids[0];
                $article_info = $this->modelArticle->where("id = " . $article_first_id)->find();
                if ($article_info) {
                    $lists[$k]['title'] = $article_info['title'];
                    $lists[$k]['cover'] = $article_info['cover'];
                    $lists[$k]['author'] = $article_info['author'];
                    $lists[$k]['source_url'] = $article_info['source_url'];
                    $lists[$k]['summary'] = $article_info['summary'];
                }
                $lists[$k]['articles'] = collection($this->modelArticle->where("id IN (" . $v['articles_ids'] . ")")->select())->toArray();
            }
            $total = count($lists);
            $result = array("total" => $total, "rows" => $lists);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = explode(',', $ids);

        foreach ($ids as $v) {

            $this->delOne($v);
        }

        $this->success();
    }

    public function delOne($ids = null)
    {
        $draft_find = $this->model->where("id =" . $ids)->find();
        if (!$draft_find) {
            $this->error('未找到草稿id：' . $ids);
        }

        $draft_detail = $draft_find->toArray();

        //    print_r($draft_detail);
        $deleteDraftResult = $this->model->deleteDraft($this->access_token, $draft_detail['media_id']);

        if ($deleteDraftResult['errmsg'] === 'ok' && $draft_detail['articles_ids']) {
            // 更新所有文章为待发布
            $this->modelArticle->where("id IN (" . $draft_detail['articles_ids'] . ")")->update(array('state' => 0));

            $this->model->where("id =" . $ids)->delete();
        }
    }

    public function edit($ids = null)
    {
        if ($this->request->isAjax()) {
            $ids = $this->request->param('ids');
            $towxname = input("post.towxname");
            $draft_find = $this->model->where("id =" . $ids)->find();
            $draft_detail = $draft_find->toArray();
            $media_id = $draft_detail['media_id'];
            $media_json = '{
   "towxname":"' . $towxname . '",
   "mpnews":{
            "media_id":"' . $media_id . '"
             },
   "msgtype":"mpnews"
}';
            $url_material = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=" . $this->access_token; //调用该接口需https协议
            $curl_info = curl_post($url_material, 'post', $media_json);

            $arr = json_decode($curl_info, true);
            if ($arr['errcode'] == 0) {
                $this->success();
            } else {
                $this->error($arr['errmsg']);
            }

        }
        return $this->view->fetch();
    }

    //http://www.guoman-admin.com/panel.php/wechat/drafts/mpweixin_article_detail?id=4
    public function mpweixin_article_detail()
    {

        $ids = 25;
        $draft_find = $this->model->where("id =" . $ids)->find();
        $draft_detail = $draft_find->toArray();
        $media_id = $draft_detail['media_id'];
        $media_json = '{"media_id":"' . $media_id . '"}';
        $url_material = "https://api.weixin.qq.com/cgi-bin/draft/get?access_token=" . $this->access_token; //调用该接口需https协议
        $curl_info = curl_post($url_material, 'post', $media_json);
        print_r($curl_info);
    }
    //http://www.guoman-admin.com/panel.php/wechat/drafts/test?id=4
    public function test(){
        $url = "https://www.toutiao.com/c/user/token/MS4wLjABAAAA0xnasH661_bC37gW6r1kTter-ILXlY5mZE98MeCnOhU/?source=mine_home&tab=wtt";
        $rs = file_get_contents($url);
    }


}
